//
//  BIZSynchronousTutorialWordDetailVC.m
//  biz
//
//  Created by LXL on 2017/7/6.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZSynchronousTutorialWordDetailVC.h"
#import "BIZSynchronousTutorialModel.h"
#import "BIZSynchronousTutorialVideoDetailVC.h"
#import "BIZSynchronousDetailCell.h"

@interface BIZSynchronousTutorialWordDetailVC ()<UIWebViewDelegate, UITableViewDelegate, UITableViewDataSource, BIZSynchronousDetailCellDelegate>
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomBtnTopHegiht;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomBtnHegiht;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) BIZSynchronousDetailCell *synchronousDetailCell;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomBtnBottomHeight;
@property (nonatomic, copy) NSString *contentStr;
@end

@implementation BIZSynchronousTutorialWordDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if (self.detailType == BIZSynchronousTutorialWordDetailTypeSynchronous) {
        self.title = @"最新财税政策同步辅导详情";
        
    } else if (self.detailType == BIZSynchronousTutorialWordDetailTypeRelated) {
        self.title = @"专家解读详情";
        
    } else {
       self.title = @"政策问答详情";
    }
    

    [self configTableView];

    [self loadWebViewData];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
     [self.synchronousDetailCell stopLoading];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.synchronousDetailCell stopLoading];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    if (self.detailType != BIZSynchronousTutorialWordDetailTypeSynchronous) {
        self.bottomBtnTopHegiht.constant = 0;
        self.bottomBtnHegiht.constant = 0;
        self.bottomBtnBottomHeight.constant = 0;
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    self.synchronousDetailCell = [tableView dequeueReusableCellWithIdentifier:@"BIZSynchronousDetailCell"];
    if (self.detailType == BIZSynchronousTutorialWordDetailTypeAnswer) {
        self.synchronousDetailCell.titleLabel.text = self.synchronousAnswerModel.question;
        self.synchronousDetailCell.dateLabel.text = [NSDate stringFromTimestamp:self.synchronousAnswerModel.createDate];
        
    } else {
        self.synchronousDetailCell.titleLabel.text = self.synchronousTutorialModel.title;
        self.synchronousDetailCell.dateLabel.text = [NSDate stringFromTimestamp:self.synchronousTutorialModel.createDate];
        
    }
    
    self.synchronousDetailCell.delegate = self;
    self.synchronousDetailCell.html = self.contentStr;
    return self.synchronousDetailCell;
}



- (void)loadWebViewData {
    
    NSMutableDictionary *paramDic = [NSMutableDictionary dictionary];

    NSString *url = @"";
    if (self.detailType == BIZSynchronousTutorialWordDetailTypeSynchronous) {
         paramDic[@"uuid"] = self.synchronousTutorialModel.uuid;
         paramDic[@"type"] = @"policy";
         url = kSynchronousTutorialDetailUrl;
    } else if (self.detailType == BIZSynchronousTutorialWordDetailTypeRelated) {
         paramDic[@"uuid"] = self.synchronousTutorialModel.uuid;
         url = kRelevantAnalysisDetailUrl;
    } else {
        url = kSynchronousTutorialAnswerDetailUrl;
        paramDic[@"uuid"] = self.synchronousAnswerModel.uuid;
    }
   
    
    [MBProgressHUD showAnimationHUD];
    [RequestTool POST:url parameters:paramDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        DeLog(@"政策问答列表》》》》》》》》》》：%@", responseObject);
        [MBProgressHUD hideHUD];
        //        [self stopRefresh];
        NSDictionary *data = responseObject[@"data"];
        NSInteger code = [responseObject[@"code"] integerValue];
        NSString *des = responseObject[@"des"];

        if (code == 1) {
            if (self.detailType == BIZSynchronousTutorialWordDetailTypeSynchronous) {
                 self.contentStr = data[@"policy"];
                
            } else if (self.detailType == BIZSynchronousTutorialWordDetailTypeRelated) {
                self.contentStr = data[@"content"];
                
            } else {
                self.contentStr = data[@"answer"];
            }
           
            [self.tableView reloadData];
            
        } else {
            [MBProgressHUD showErrorMessage:des];
            if (code == 2) {
                BIZLoginViewController *vc = [BIZLoginViewController viewControllerFromStoryboard];
                MPWeakSelf(self);
                vc.loginSuccessBlock = ^(){
                    [weakself loadWebViewData];
                };
                [self presentViewController:vc animated:YES completion:nil];
            }
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        DeLog(@"政策问答列表错误》》》》》》》》》》：%@", error);
        if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
            [MBProgressHUD showErrorMessage:@"网络不给力"];
        }
        else {
            [MBProgressHUD showErrorMessage:@"服务器繁忙"];
        }
    }];

}

- (void)configTableView {
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 146;
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [self.tableView registerNib:[UINib nibWithNibName:@"BIZSynchronousDetailCell" bundle:nil] forCellReuseIdentifier:@"BIZSynchronousDetailCell"];
    
}

- (void)synchronousDetailCell:(BIZSynchronousDetailCell *)cell heightDidChange:(CGFloat)height {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)seeSynthronousTutorial {
    BIZSynchronousTutorialVideoDetailVC *playHomeVC = [[BIZSynchronousTutorialVideoDetailVC alloc]initWithUuid:self.synchronousTutorialModel.videoId];
    playHomeVC.synchronousTutorialModel = self.synchronousTutorialModel;
    [self.navigationController pushViewController:playHomeVC animated:YES];
}


@end
